import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false
import {postRequest} from "./util/api";
import {putRequest} from "./util/api";
import {getRequest} from "./util/api";
import {deleteRequest} from "./util/api";
//自定义axios插件
Vue.prototype.postRequest = postRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.deleteRequest = deleteRequest;
import dayjs from "dayjs";
Vue.prototype.$moment = dayjs;
//引入element ui样式
import store from './store/index'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import {initMenu} from "./util/menu";
//使用element ui
Vue.use(ElementUI);

Vue.filter("date", function(value, formatStr = "YYYY-MM-DD") {
    return dayjs(value).format(formatStr);
});

Vue.filter("dateTime", function(value, formatStr = "YYYY-MM-DD HH:mm:ss") {
    return dayjs(value).format(formatStr);
});
// 路由守卫
router.beforeEach((to, from, next) => {
    if (window.sessionStorage.getItem("tokenStr")) {
        initMenu(router, store)
        if (!window.sessionStorage.getItem("user")) {
            return getRequest('/api/admin/info').then(resp => {
                if (resp) {
                    //存入用户信息
                    window.sessionStorage.setItem("user", JSON.stringify(resp));
                    next({ path: "/home" });
                }
            });
        }
        next();
    } else {
        if (to.path=='/'){
            next({to})
        }else {
            next('/?redirect='+to.path)
        }
    }
})
/* eslint-disable no-new */
//runtime
new Vue({
    router,
    store,
    render: h => h(App)
}).$mount("#app")

